package com.tich.user.controller;

import cn.hutool.core.bean.BeanUtil;

import com.tich.api.domain.vo.UserSimpleVO;
import com.tich.constant.ResponseMessageConstant;
import com.tich.context.UserContext;
import com.tich.result.Result;
import com.tich.user.domain.dto.UserDTO;
import com.tich.user.domain.dto.UserInfoDTO;
import com.tich.user.domain.dto.UserLoginDTO;
import com.tich.user.domain.dto.UserRegisterDTO;
import com.tich.user.domain.entity.User;
import com.tich.user.domain.entity.UserInfo;
import com.tich.user.domain.vo.UserLoginVO;
import com.tich.user.domain.vo.UserProfileVO;
import com.tich.user.service.IUserInfoService;
import com.tich.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register/PC")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册:{}", userRegisterDTO);
        userService.register(userRegisterDTO);
        return Result.success(ResponseMessageConstant.REGISTER_SUCCESS);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     * */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录，用户信息{}",userLoginDTO);
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Result.success(ResponseMessageConstant.LOGIN_SUCCESS,userLoginVO);
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/user")
    public Result<UserProfileVO> queryUserById(){
        log.info("查询用户信息");
        return Result.success(userService.queryUserById());
    }

    /**
     * 修改用户信息
     * @param userDTO
     * @return
     */
    @PutMapping("/user")
    public Result updateUserById(@RequestBody UserDTO userDTO){
        log.info("修改用户信息: userDTO:{}",userDTO);
        User user = BeanUtil.toBean(userDTO, User.class).setId(UserContext.getCurrentId());
        userService.updateById(user);
        return Result.success(ResponseMessageConstant.UPDATE_SUCCESS);
    }

    /**
     * 查询用户详细信息
     * @return
     */
    @GetMapping("/info")
    public Result<UserInfo> queryUserInfoById(){
        log.info("查询用户详细信息:{}",UserContext.getCurrentId());
        return Result.success(userInfoService.queryUserInfoById());
    }

    /**
     * 批量查询用户
     * @param userIds
     * @return
     */
    @GetMapping("/users")
    public List<UserSimpleVO> getSimpleUsersByIds(@RequestParam("userIds") List<Long> userIds) {
        List<User> users = userService.listByIds(userIds);
        return users.stream()
                .map(user -> new UserSimpleVO(
                        user.getId(),
                        user.getUsername(),
                        user.getImage()
                ))
                .collect(Collectors.toList());
    }
    /**
     * 修改用户详细信息
     * @param userInfoDTO
     * @return
     */
    @PutMapping("/userInfo")
    public Result updateUserInfoById(@RequestBody UserInfoDTO userInfoDTO){
        log.info("修改用户详细信息userInfoDTO:{}",userInfoDTO);
        UserInfo userInfo = BeanUtil.toBean(userInfoDTO, UserInfo.class).setUserId(UserContext.getCurrentId());
        userInfoService.updateById(userInfo);
        return Result.success(ResponseMessageConstant.UPDATE_SUCCESS);
    }

    /**
     * 删除用户
     * @return
     */
    //TODO

    @DeleteMapping
    public Result removeUserById(){
        Long userId = UserContext.getCurrentId();
        userService.deleteById(userId);
        userInfoService.deleteById(userId);
        return Result.success(ResponseMessageConstant.LOGOFF_SUCCESS);
    }


}
